#coding: utf-8

# Copyright (c) P SOFTHOUSE. All rights reserved.

import maya.cmds as cmds
import maya.mel as mel
import maya.api.OpenMaya as OpenMaya
import PSOFT.utils as utils
import PSOFT.pencil4lineRender as pencil4lineRender
_rendererName = 'pencil4line'


def _registerPencil4LineRenderer():
    args = {}
    args['renderProcedure']                     = utils.exportMel(pencil4lineRender.pencil4lineRender,
                                                        [('int', 'width'), ('int', 'height'),
                                                         ('int', 'doShadows'), ('int', 'doGlowPass'),
                                                         ('string', 'camera'), ('string', 'options')],
                                                         returnType='string')
    args['renderRegionProcedure']               = 'mayaRenderRegion'
    args['commandRenderProcedure']              = utils.exportMel(pencil4lineRender.pencil4lineBatchRender,
                                                        [('string', 'option')])
#    args['batchRenderOptionsProcedure']         = # 使用するCPU数などのダイアログ
    args['batchRenderProcedure']                = utils.exportMel(pencil4lineRender.pencil4lineBatchRender,
                                                        [('string', 'option')])
    args['batchRenderOptionsStringProcedure']   = utils.exportMel(pencil4lineRender.pencil4lineBatchRenderOptionsString, returnType='string')
    args['cancelBatchRenderProcedure']          = utils.exportMel(pencil4lineRender.pencil4lineBatchStop)
    args['showBatchRenderProcedure']            = 'batchRender -showImage true'

    cmds.renderer(_rendererName, rendererUIName = 'Pencil+ 4 Line', **args)

    maya_version = utils.getMayaVersion()
    if maya_version >= 2017:
        args['renderSequenceProcedure'] = 'mayaRenderSequence'
#    if maya_version > 2018:
#        cmds.renderer(_rendererName, edit=True, supportColorManagement=True)


    cmds.renderer(_rendererName, edit=True, addGlobalsNode = 'defaultRenderGlobals')    # 選択されたときにシーンに追加するノード（自前で定義したノードを指定可）
    cmds.renderer(_rendererName, edit=True, addGlobalsNode = 'defaultResolution')       # 選択されたときにシーンに追加するノード（自前で定義したノードを指定可）
    cmds.renderer(_rendererName, edit=True, addGlobalsTab = ('Common',
                                                             'createMayaSoftwareCommonGlobalsTab',
                                                             'updateMayaSoftwareCommonGlobalsTab'))


def _unregisterPencil4LineRenderer():
    cmds.renderer (_rendererName, edit=True, unregisterRenderer=True)



def registerPencil4LineRenderer(to_register):
    is_registered = _rendererName in cmds.renderer(q=True, namesOfAvailableRenderers=True)

    if bool(to_register) == bool(is_registered):
        return

    if to_register:
        _registerPencil4LineRenderer()
        if not cmds.about(batch=True):
            pencil4lineRender.listen.commandPortListenStart()
#        print "registerPencil4LineRenderer !!"

    else:
        _unregisterPencil4LineRenderer()
        if not cmds.about(batch=True):
            pencil4lineRender.listen.commandPortListenEnd()
        else:
            pencil4lineRender.client.commandPortClientEnd()
#        print "unregisterPencil4LineRenderer !!"

def _initialize():
    registerPencil4LineRenderer(True)
    utils.exportMel(registerPencil4LineRenderer, [('int', 'is_register')], procName=True)

def initialize():
    # Maya標準のレンダラより後に登録されるように、遅延評価する
    cmds.evalDeferred('PSOFT.pencil4lineRenderer._initialize()')

